package Dao.impl;

import Dao.BaseDao;
import Dao.BookDao;
import pojo.Book;

import java.util.List;

public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book (id,name,author,price,sales,stock,img_path) values(?,?,?,?,?,?,?)";
       return updateForOne(sql,book.getId(),book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),
        book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id = ?";
        return updateForOne(sql,id);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set name=?,author=?,price=?,sales=?,stock=?,img_path=? where id =?";
        return updateForOne(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),
                book.getImgPath(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book where id = ?";
        return  queryForOne(Book.class,sql,id);
    }

    @Override
    public List<Book> queryAllBook() {
        String sql = "select id,name,author,price,sales,stock,img_path imgPath from t_book";
        return  queryMore(Book.class,sql);
    }

    @Override
    public Long queryForPageTotalCount() {
        String sql = "select count(*) from t_book";
        return getValue(sql);
    }

    @Override
    public List<Book> queryForitems(int begin,int pageSize) {
        String sql = "select id,name,author,price,sales,stock,img_path from t_book limit ?,?     ";
        return queryMore(Book.class,sql,begin,pageSize);
    }

    @Override
    public Long queryForPageTotalCount(int min, int max) {
        String sql = "select count(*) from t_book where price between ? and ?";
        return getValue(sql,min,max);
    }

    @Override
    public List<Book> qurtyForPageItems(int begin, int pageSize, int min, int max) {
        String sql = "select id,name,author,price,sales,stock,img_path from " +
                "t_book where price between ? and ? order by price limit ?,? ";
        return queryMore(Book.class,sql,min,max,begin,pageSize);

    }
}
